import RxElement from "../../utils/RxElement"
import RxStore from "../../utils/RxStore"

const ID_TABWRAP = 'tab_wrap'
const WRAP_CHANGE_SELECT = 'select'
let glob_select = 1

export default () => {
  RxElement.getElButtonIdFun(ID_TABWRAP, (e) => {
    e = e || {}
    let target = e.target || {}
    // console.log('e=', e)


    // console.log('target.class=', target.className )
    if (target.className == 'text') {
      target = target.parentNode
    }
    const dataset = target.dataset || {}
    // console.log('target=', target, dataset, JSON.stringify(dataset))

    const n = dataset.n || -1
    // console.log('n-----=', n, glob_select)


    if (glob_select != n) {
      if (glob_select > -1) {
        // console.log('改变旧值')
        _getTabItem(glob_select)
      }
      glob_select = n
      target.classList.add(WRAP_CHANGE_SELECT)

      RxStore.emit('rxtab', n)
    }
  })
}

function _getTabItem(n) {
  const e = RxElement.getElButtonId(ID_TABWRAP)
  // console.log('_getTabItem e', e, e.childNodes)

  const wrapArray = e.childNodes || []
  const len = wrapArray.length

  let i = 0
  for (i = 0; i < len; i++) {
    let tabItem = wrapArray[i] || {}
    // console.log(i, 'tabItem=', tabItem)
    if (tabItem.className == 'text') {
      tabItem = tabItem.parentNode
    }
    const dataset = tabItem.dataset || {}
    const dn = dataset.n || -1
    // console.log('i=', i, dn)
    if (dn > -1 && dn == n) {
      tabItem.classList.remove(WRAP_CHANGE_SELECT)
      // console.log(tabItem)
      break
    }
  }

  // console.log('i====', i, wrapArray)
}